package com.ego.product.interceptor;

import com.ego.common.constant.MemberConstant;
import com.ego.common.to.member.MemberTo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: ego
 * @description: 订单服务拦截器
 * @author: KamTang
 * @create: 2022-12-20 12:48
 **/
public class LoginInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberTo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //String uri = request.getRequestURI();
        //// 解决远程服务调用之间出现拦截，导致返回类型无法转换
        //AntPathMatcher antPathMatcher = new AntPathMatcher();
        //boolean match1 = antPathMatcher.match("/order/status/**", uri);
        //boolean match2 = antPathMatcher.match("/order/pay-info/**", uri);
        //if (match1 || match2) {
        //    return true;
        //}

        MemberTo member = (MemberTo) request.getSession().getAttribute(MemberConstant.LOGGED_USER);
        if (member != null) {
            threadLocal.set(member);
            return true;
        } else {
            response.sendRedirect("http://egomall.shop:8006/login");
            return false;
        }

    }
}
